from tkinter import *


def show_pos_event(event):
    """鼠标移动事件"""
    print("组件：{}, x={}，y={}".format(event.widget, event.x, event.y))


def show_all_event(event):
    """展示所有事件"""
    for attr in dir(event):
        if not attr.startswith('__'):
            print(attr, '=>', getattr(event, attr))


def on_key_press(event):
    """键盘按下事件"""
    print("鼠标按下：", event.char)


def on_arrow_key(event):
    print("方向键按下：", event.char)


def on_return_key(event):
    print("确认键按下：", event.char)


def on_left_click(event):
    print("鼠标左键按下：")
    show_pos_event(event)


def on_right_click(event):
    print("鼠标右键按下：")
    show_pos_event(event)


def on_middle_click(event):
    print("鼠标中键按下：")
    show_pos_event(event)


def on_left_drag(event):
    print("鼠标左键拖动：")
    show_pos_event(event)


def on_double_left_click(event):
    print("鼠标左键双击")
    show_pos_event(event)
    tkroot.quit()


tkroot = Tk()
labelfont = ('courier', 20, 'bold')
widget = Label(tkroot, text="绑定事件")
widget.config(bg='red', font=labelfont)
widget.config(height=5, width=20)
widget.pack(expand=YES, fill=BOTH)

# 鼠标左键单击
widget.bind('<Button-1>', on_left_click)
# 鼠标右键单击
widget.bind('<Button-3>', on_right_click)
# 鼠标中键单击
widget.bind('<Button-2>', on_middle_click)
# 鼠标左键双击
widget.bind('<Double-1>', on_double_left_click)
# 左键拖动
widget.bind('<B1-Motion>', on_left_drag)
# 按下所有键盘键
widget.bind('<KeyPress>', on_key_press)
# 按下箭头键
widget.bind('<Up>', on_arrow_key)
# 按下回车键
widget.bind('<Return>', on_return_key)
widget.focus()
tkroot.title('演示绑定事件')
tkroot.mainloop()
